package han.parser;

import han.entity.InvestBasicEntity;
import han.entity.InvestCompanyInfoEntity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author: Han
 * @date: 2018/7/12 14:42
 * @dscription: TODO
 */
public class InvestOrganizationInfoParser {

    public static InvestBasicEntity getBasic(String rowStr) {
        JSONObject dataJsonObj = new JSONObject(rowStr).getJSONObject("data");
        long krId = 0;
        try {
            krId = dataJsonObj.getLong("id");
        } catch (JSONException ignore) {
        }
        String name = "-";
        try {
            name = dataJsonObj.getString("nameAbbr");
        } catch (JSONException ignore) {
        }
        String fullName = "-";
        try {
            fullName = dataJsonObj.getString("name");
        } catch (JSONException ignore) {
        }
        String intro = "-";
        try {
            intro = dataJsonObj.getString("intro");
        } catch (JSONException ignore) {
        }
        StringBuilder emailSB = new StringBuilder();
        StringBuilder phoneSB = new StringBuilder();
        StringBuilder addressSB = new StringBuilder();
        try {
            JSONArray addArray = dataJsonObj.getJSONArray("addresses");
            for (int i = 0; i < addArray.length(); i++) {
                JSONObject tempJsonObj = addArray.getJSONObject(i);
                String addr = tempJsonObj.getString("address");
                if (addr.isEmpty()) {
                    addr = "-";
                }
                addressSB.append(addr).append(";");
                String email = tempJsonObj.getString("email");
                if (email.isEmpty() || !email.contains("@")) {
                    email = "-";
                }
                emailSB.append(email).append(";");
                String phone = tempJsonObj.getString("phone");
                if (phone.isEmpty()) {
                    phone = "-";
                }
                phoneSB.append(phone).append(";");
            }
        } catch (JSONException ignore) {
        }
        if (addressSB.length() == 0) {
            addressSB.append("-");
        }
        if (emailSB.length() == 0) {
            emailSB.append("-");
        }
        if (phoneSB.length() == 0) {
            phoneSB.append("-");
        }
        //官网
        String website = "-";
        try {
            website = dataJsonObj.getString("website");
        } catch (JSONException ignore) {
        }
        //标签
        StringBuilder industrySB = new StringBuilder();
        try {
            JSONArray investArray = dataJsonObj.getJSONArray("investedIndustry");
            for (int iIndex = 0; iIndex < investArray.length(); iIndex++) {
                JSONObject tempJson = investArray.getJSONObject(iIndex);
                String iName = tempJson.getString("name");
                industrySB.append(iName).append(",");
            }
        } catch (JSONException ignore) {
        }
        if (industrySB.length() == 0) {
            industrySB.append("-");
        }
        return new InvestBasicEntity(0, krId + "", name, fullName, intro, addressSB
                .toString(), emailSB.toString(), phoneSB.toString(), website, industrySB.toString());
    }

    public static String getMember(String rowStr) {
        StringBuilder memberSB = new StringBuilder();
        try {
            JSONArray memberArray = new JSONObject(rowStr).getJSONObject("data").getJSONArray("member");
            for (int i = 0; i < memberArray.length(); i++) {
                JSONObject tempJsonObj = memberArray.getJSONObject(i);
                String title = "-";
                try {
                    title = tempJsonObj.getString("title");
                    if (title.isEmpty()) {
                        title = "-";
                    }
                } catch (Exception ignore) {
                }
                JSONObject userInfoJsonObj = tempJsonObj.getJSONObject("userInfo");
                String name = "-";
                try {
                    name = userInfoJsonObj.getString("name");
                    if (name.isEmpty()) {
                        name = "-";
                    }
                } catch (Exception ignore) {
                }
                String intro = "-";
                try {
                    intro = userInfoJsonObj.getString("intro");
                    if (intro.isEmpty()) {
                        intro = "-";
                    }
                } catch (Exception ignore) {
                }
                memberSB.append("姓名:").append(name).append("; 头衔:").append(title).append("; 简介:").append(intro).append
                        ("\n");
            }
        } catch (JSONException ignore) {
        }
        if (memberSB.length() == 0) {
            memberSB.append("-");
        }

        return memberSB.toString();
    }

    public static InvestCompanyInfoEntity getOrgInfoM(String str) {
        JSONObject dataJson = new JSONObject(str).getJSONObject("data");
        //简介
        String intro = dataJson.getString("intro");
        //id
        long id = dataJson.getLong("id");
        //成员总页数,一页20个
        JSONObject investorListJson = dataJson.getJSONObject("investorList");
        int totalPages = investorListJson.getInt("totalPages");
        if (totalPages % 6 == 0) {
            totalPages = totalPages / 6;
        } else {
            totalPages = totalPages / 6 + 1;
        }
        //官网
        String website = dataJson.getString("website");
        //简称
        String name = dataJson.getString("nameAbbr");
        //标签
        StringBuilder industrySB = new StringBuilder();
        JSONArray industryArray = dataJson.getJSONArray("investIndustryVOList");
        for (int iIndex = 0; iIndex < industryArray.length(); iIndex++) {
            JSONObject tempJson = industryArray.getJSONObject(iIndex);
            String iName = tempJson.getString("name");
            industrySB.append(name).append(",");
        }
        InvestCompanyInfoEntity entity = new InvestCompanyInfoEntity();
        entity.setName(name);
        //entity.set

        return entity;
    }
}
